package StudyCode.summary.mycollection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsTrueIfArrayAFromPositionStartContainsAllElementsOfArrayBInSequentialOrder {
	public static void main(String[] argv) {
		byte[] arra = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int start = 2;
		byte[] arrb = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		System.out.println(containsAt(arra, start, arrb));
	}

	public static boolean containsAt(byte[] arra, int start, byte[] arrb) {
		return countSameElements(arra, start, arrb) == arrb.length;
	}

	public static boolean containsAt(char[] arra, int start, char[] arrb) {
		return countSameElements(arra, start, arrb) == arrb.length;
	}

	public static int countSameElements(byte[] arra, int start, byte[] arrb) {

		int k = 0;
		int limit = arra.length - start;

		if (limit > arrb.length) {
			limit = arrb.length;
		}

		for (int i = 0; i < limit; i++) {
			if (arra[i + start] == arrb[i]) {
				k++;
			} else {
				break;
			}
		}

		return k;
	}

	public static int countSameElements(char[] arra, int start, char[] arrb) {

		int k = 0;
		int limit = arra.length - start;

		if (limit > arrb.length) {
			limit = arrb.length;
		}

		for (int i = 0; i < limit; i++) {
			if (arra[i + start] == arrb[i]) {
				k++;
			} else {
				break;
			}
		}

		return k;
	}
}
